/*********************************************************************************************************
**
**                                   GJB 标准测试集
**
**                               Copyright All Rights Reserved
**
**--------------文件信息--------------------------------------------------------------------------------
**
** 文   件   名: gjb_S0100902GN_1.c
**
** 文件创建日期: 2021 年 1 月 22 日
**
** 描        述: 以永久阻塞方式等待条件变量功能测试
*********************************************************************************************************/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>

//#include "gjb.h"

static int  err_count_cv_9022;

pthread_cond_t cond1_cv_9022 = PTHREAD_COND_INITIALIZER;                          /* cond used by the two threads */

pthread_mutex_t mutex1_cv_9022 = PTHREAD_MUTEX_INITIALIZER;                       /* cond used by the two threads */

void * thread1_cv_9022(void *tmp)
{
    int    rc = 0;

    /*
     * acquire the mutex
     */
    rc  = pthread_mutex_lock(&mutex1_cv_9022);
    if(rc != 0) {
        printf("test failed.pthread_mutex_lock\n");
        err_count_cv_9022++;
        return (void *)-1;
    }


    rc = pthread_cond_wait(&cond1_cv_9022, &cond1_cv_9022);
    if (rc == EINVAL) {
        printf("test 2 pass,pthread_cond_wait return EINVAL \n");

    } else {
        printf("test 2 failed.pthread_cond_wait should return EINVAL  , but return %d\n",rc);
        err_count_cv_9022++;
        return (void *)-1;
    }

    rc = pthread_cond_wait(NULL, &mutex1_cv_9022);
    if (rc == EINVAL ) {
        printf("test 3 pass,pthread_cond_wait return EINVAL \n");

    } else {
        printf("test 3 failed.pthread_cond_wait should return EINVAL  , but return %d\n",rc);
        err_count_cv_9022++;
        return (void *)-1;
    }

    rc = pthread_cond_wait(&cond1_cv_9022,NULL);
    if(rc == EINVAL ) {
        printf("test 4 pass,pthread_cond_wait return EINVAL \n");

    } else {
        printf("test 4 failed.pthread_cond_wait should return EINVAL  , but return %d\n",rc);
        err_count_cv_9022++;
        return (void *)-1;
    }

    rc = pthread_cond_wait(&cond1_cv_9022,&mutex1_cv_9022);
    if(rc != 0 ) {
        printf("test 5 failed.pthread_cond_wait should return 0  , but return %d\n",rc);
        err_count_cv_9022++;
        return (void *)-1;
    }

    rc = pthread_mutex_unlock(&mutex1_cv_9022);
    if(rc != 0) {
        printf("test failed.pthread_mutex_unlock\n");
        err_count_cv_9022++;
        return (void *)-1;
    }

    return NULL;
}

int gjb_S0100902GN_2(int argc, char **argv)
{
    pthread_t low_id;
    pthread_mutex_t mutex2;
    int       rc = 0;

    pthread_mutex_init(&mutex2,NULL);

    /*
     * Create a new thread with default attributes
     */
    rc = pthread_create(&low_id, NULL, thread1_cv_9022, NULL);
    if(rc != 0) {
        printf("pthread_create\n");
        goto __errno_handle;
    }

    /*
     * Let the other thread run
     */
    sleep(2);

    /*
     * acquire the mutex
     */
    rc  = pthread_mutex_lock(&mutex2);
    if(rc != 0) {
        printf("test failed.pthread_mutex_lock(2)\n");
        goto __errno_handle;
    }

    rc = pthread_cond_wait(&cond1_cv_9022,&mutex2);
    if(rc == EINVAL ) {
        printf("test 5 pass,pthread_cond_wait return EINVAL \n");

    } else {
        printf("test 5 failed.pthread_cond_wait should return EINVAL  , but return %d\n",rc);
        goto __errno_handle;
    }

    rc = pthread_mutex_unlock(&mutex2);
    if(rc != 0) {
        printf("test failed.pthread_mutex_unlock\n");
        goto __errno_handle;
    }



    sleep(1);
    pthread_kill(low_id, SIGTERM);

    if (err_count_cv_9022) {
      goto __errno_handle;
    }

    printf("<RSLT>..................................................[(0)]\n \n \n");

    return  (0);

__errno_handle:
    printf("<RSLT>..................................................[(-1)]\n \n \n");
    return  (-1);
}




